home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch9 / TweenRob.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-05-29  |  20.7 KB  |  683 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmTweenRob 
  4.    Caption         =   "TweenRob []"
  5.    ClientHeight    =   4590
  6.    ClientLeft      =   2040
  7.    ClientTop       =   1035
  8.    ClientWidth     =   4635
  9.    LinkTopic       =   "Form1"
  10.    PaletteMode     =   1  'UseZOrder
  11.    ScaleHeight     =   306
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   309
  14.    Begin VB.TextBox txtNumTweens 
  15.       Height          =   285
  16.       Left            =   4200
  17.       TabIndex        =   11
  18.       Text            =   "4"
  19.       Top             =   120
  20.       Width           =   375
  21.    End
  22.    Begin VB.CommandButton cmdTween 
  23.       Caption         =   "Tween"
  24.       Default         =   -1  'True
  25.       Height          =   495
  26.       Left            =   3480
  27.       TabIndex        =   10
  28.       Top             =   600
  29.       Width           =   975
  30.    End
  31.    Begin VB.TextBox txtFramesPerSecond 
  32.       Height          =   285
  33.       Left            =   4200
  34.       TabIndex        =   9
  35.       Text            =   "20"
  36.       Top             =   1740
  37.       Width           =   375
  38.    End
  39.    Begin VB.CommandButton cmdPlay 
  40.       Caption         =   "Play"
  41.       Height          =   495
  42.       Left            =   3480
  43.       TabIndex        =   7
  44.       Top             =   3480
  45.       Width           =   975
  46.    End
  47.    Begin VB.OptionButton optPlay 
  48.       Caption         =   "Reversing"
  49.       Height          =   255
  50.       Index           =   2
  51.       Left            =   3360
  52.       TabIndex        =   4
  53.       Top             =   3000
  54.       Width           =   1095
  55.    End
  56.    Begin VB.OptionButton optPlay 
  57.       Caption         =   "Looping"
  58.       Height          =   255
  59.       Index           =   1
  60.       Left            =   3360
  61.       TabIndex        =   3
  62.       Top             =   2640
  63.       Width           =   1095
  64.    End
  65.    Begin VB.OptionButton optPlay 
  66.       Caption         =   "Once"
  67.       Height          =   255
  68.       Index           =   0
  69.       Left            =   3360
  70.       TabIndex        =   2
  71.       Top             =   2280
  72.       Value           =   -1  'True
  73.       Width           =   1095
  74.    End
  75.    Begin VB.HScrollBar sbarFrame 
  76.       Height          =   255
  77.       Left            =   0
  78.       Max             =   1
  79.       Min             =   1
  80.       TabIndex        =   1
  81.       Top             =   3960
  82.       Value           =   1
  83.       Width           =   3255
  84.    End
  85.    Begin VB.PictureBox picCanvas 
  86.       Height          =   3975
  87.       Left            =   0
  88.       ScaleHeight     =   261
  89.       ScaleMode       =   3  'Pixel
  90.       ScaleWidth      =   213
  91.       TabIndex        =   0
  92.       Top             =   0
  93.       Width           =   3255
  94.    End
  95.    Begin MSComDlg.CommonDialog dlgFile 
  96.       Left            =   3360
  97.       Top             =   3600
  98.       _ExtentX        =   847
  99.       _ExtentY        =   847
  100.       _Version        =   393216
  101.       CancelError     =   -1  'True
  102.    End
  103.    Begin VB.Label Label1 
  104.       Caption         =   "Tweens"
  105.       Height          =   255
  106.       Index           =   2
  107.       Left            =   3360
  108.       TabIndex        =   12
  109.       Top             =   120
  110.       Width           =   855
  111.    End
  112.    Begin VB.Label Label1 
  113.       Alignment       =   2  'Center
  114.       Caption         =   "Frames per Second"
  115.       Height          =   375
  116.       Index           =   1
  117.       Left            =   3360
  118.       TabIndex        =   8
  119.       Top             =   1680
  120.       Width           =   855
  121.    End
  122.    Begin VB.Label lblFrame 
  123.       Alignment       =   1  'Right Justify
  124.       BorderStyle     =   1  'Fixed Single
  125.       Caption         =   "1/1"
  126.       Height          =   255
  127.       Left            =   1680
  128.       TabIndex        =   6
  129.       Top             =   4320
  130.       Width           =   735
  131.    End
  132.    Begin VB.Label Label1 
  133.       Caption         =   "Frame:"
  134.       Height          =   255
  135.       Index           =   0
  136.       Left            =   1080
  137.       TabIndex        =   5
  138.       Top             =   4320
  139.       Width           =   495
  140.    End
  141.    Begin VB.Menu mnuFile 
  142.       Caption         =   "&File"
  143.       Begin VB.Menu mnuFileOpen 
  144.          Caption         =   "&Open..."
  145.          Shortcut        =   ^O
  146.       End
  147.       Begin VB.Menu mnuFileSave 
  148.          Caption         =   "&Save"
  149.          Shortcut        =   ^S
  150.       End
  151.       Begin VB.Menu mnuFileSaveAs 
  152.          Caption         =   "Save &As..."
  153.          Shortcut        =   ^A
  154.       End
  155.       Begin VB.Menu mnuFileSep 
  156.          Caption         =   "-"
  157.       End
  158.       Begin VB.Menu mnuFileExit 
  159.          Caption         =   "E&xit"
  160.       End
  161.    End
  162.    Begin VB.Menu mnuFileFrames 
  163.       Caption         =   "Frames"
  164.       Begin VB.Menu mnuFrameAfter 
  165.          Caption         =   "Insert &After"
  166.       End
  167.       Begin VB.Menu mnuFrameBefore 
  168.          Caption         =   "Insert &Before"
  169.       End
  170.       Begin VB.Menu mnuFrameSep 
  171.          Caption         =   "-"
  172.       End
  173.       Begin VB.Menu mnuFrameDelete 
  174.          Caption         =   "&Delete"
  175.          Enabled         =   0   'False
  176.       End
  177.    End
  178. Attribute VB_Name = "frmTweenRob"
  179. Attribute VB_GlobalNameSpace = False
  180. Attribute VB_Creatable = False
  181. Attribute VB_PredeclaredId = True
  182. Attribute VB_Exposed = False
  183. Option Explicit
  184. Private NumFrames As Integer
  185. Private Frames() As New Robot
  186. Private SelectedFrame As Integer
  187. Private SelectingFrame As Boolean
  188. Private FileName As String
  189. Private FileTitle As String
  190. Private DataModified As Boolean
  191. Private Playing As Boolean
  192. Private NumPlayed As Long
  193. Private Dragging As Boolean
  194. Private DragPoint As Integer
  195. Private DragX As Integer
  196. Private DragY As Integer
  197. Private AnchorX As Integer
  198. Private AnchorY As Integer
  199. ' Make the tweens.
  200. Private Sub cmdTween_Click()
  201. Dim num_tweens As Integer
  202. Dim num_frames As Integer
  203. Dim frame1 As Integer
  204. Dim frame2 As Integer
  205. Dim frac1 As Single
  206. Dim i As Integer
  207. Dim frame As Integer
  208.     ' See how many tweens to make.
  209.     If Not IsNumeric(txtNumTweens.Text) Then _
  210.         txtNumTweens.Text = "4"
  211.     num_tweens = txtNumTweens.Text
  212.     If num_tweens < 1 Then num_tweens = 1
  213.     ' Make room for the new frames.
  214.     num_frames = num_tweens * (NumFrames - 1) + NumFrames
  215.     ReDim Preserve Frames(1 To num_frames)
  216.     ' Spread the original frames out.
  217.     For frame = NumFrames To 2 Step -1
  218.         frame1 = num_tweens * (frame - 1) + frame
  219.         Frames(frame1).CopyFrame Frames(frame)
  220.     Next frame
  221.     ' Make the tweens.
  222.     For frame = 1 To NumFrames - 1
  223.         frame1 = num_tweens * (frame - 1) + frame
  224.         frame2 = frame1 + num_tweens + 1
  225.         For i = frame1 + 1 To frame2 - 1
  226.             frac1 = (frame2 - i) / (frame2 - frame1)
  227.             Frames(i).Tween frac1, _
  228.                 Frames(frame1), Frames(frame2)
  229.         Next i
  230.     Next frame
  231.     NumFrames = num_frames
  232.     sbarFrame.Max = NumFrames
  233.     SelectFrame num_tweens * (SelectedFrame - 1) + _
  234.         SelectedFrame
  235.     DataModified = True
  236.     Caption = "TweenRob*[" & FileTitle & "]"
  237. End Sub
  238. ' Convert (X, Y) into the point in the direction
  239. ' of (X, Y) that is the correct distance from the
  240. ' anchor point. For example, when dragging an
  241. ' elbow, the point should be UpperArmLength distance
  242. ' from the shoulders.
  243. Private Sub AdjustPoint(x As Single, y As Single)
  244. Dim dist As Single
  245. Dim factor As Single
  246. Dim dx As Single
  247. Dim dy As Single
  248.     ' Heads have no anchor point.
  249.     If DragPoint = part_Head Then
  250.         DragX = x
  251.         DragY = y
  252.         Exit Sub
  253.     End If
  254.     dx = x - AnchorX
  255.     dy = y - AnchorY
  256.     dist = Sqr(dx * dx + dy * dy)
  257.     Select Case DragPoint
  258.         Case part_Lelbow, part_RElbow
  259.             factor = Frames(1).UpperArmLength / dist
  260.         Case part_LHand, part_RHand
  261.             factor = Frames(1).LowerArmLength / dist
  262.         Case part_LKnee, part_RKnee
  263.             factor = Frames(1).UpperLegLength / dist
  264.         Case part_LFoot, part_RFoot
  265.             factor = Frames(1).LowerLegLength / dist
  266.     End Select
  267.     DragX = AnchorX + dx * factor
  268.     DragY = AnchorY + dy * factor
  269. End Sub
  270. ' Return true if the data has not been modified,
  271. ' or the user has saved the changes, or the user
  272. ' wants to lose the changes.
  273. Private Function DataSafe() As Boolean
  274. Dim ans As Integer
  275.     Do While DataModified
  276.         ans = MsgBox("The data has been modified." & _
  277.             " Do you want to save the changes?", _
  278.             vbYesNoCancel)
  279.         If ans = vbCancel Then Exit Do
  280.         If ans = vbNo Then
  281.             DataSafe = True
  282.             Exit Function
  283.         End If
  284.             
  285.         ' Otherwise save the data.
  286.         If FileName <> "" Then
  287.             mnuFileSave_Click
  288.         Else
  289.             mnuFileSaveAs_Click
  290.         End If
  291.     Loop
  292.     DataSafe = Not DataModified
  293. End Function
  294. ' Draw the highlight fot the drag.
  295. Private Sub DrawDrag()
  296.     If DragPoint = part_Head Then
  297.         picCanvas.Line (DragX - Near, DragY - Near)-Step(Near2, Near2), , BF
  298.     Else
  299.         picCanvas.Line (AnchorX, AnchorY)-(DragX, DragY)
  300.     End If
  301. End Sub
  302. ' Draw the selected configuration.
  303. Private Sub DrawSelected()
  304.     picCanvas.Cls
  305.     Frames(SelectedFrame).Draw picCanvas, True
  306. End Sub
  307. ' Save a robot script into the file.
  308. Private Sub SaveScript(ByVal file_name As String, ByVal file_title As String)
  309. Dim fnum As Integer
  310. Dim i As Integer
  311.     On Error GoTo SaveScriptError
  312.     ' Open the file.
  313.     fnum = FreeFile
  314.     Open file_name For Output As fnum
  315.     ' Write the number of frames.
  316.     Write #fnum, NumFrames
  317.     ' Write the parameters for each frame.
  318.     For i = 1 To NumFrames
  319.         Frames(i).FileWrite fnum
  320.     Next i
  321.     Close fnum
  322.     FileName = file_name
  323.     FileTitle = file_title
  324.     DataModified = False
  325.     Caption = "TweenRob [" & FileTitle & "]"
  326.     Exit Sub
  327. SaveScriptError:
  328.     Beep
  329.     MsgBox "Error saving file " & file_name & "." & _
  330.         vbCrLf & Format$(Err.Number) & " : " & _
  331.         Err.Description
  332.     Exit Sub
  333. End Sub
  334. ' Load a robot script from the file.
  335. Private Sub LoadScript(ByVal file_name As String, ByVal file_title As String)
  336. Dim fnum As Integer
  337. Dim i As Integer
  338.     On Error GoTo LoadScriptError
  339.     ' Open the file.
  340.     fnum = FreeFile
  341.     Open file_name For Input As fnum
  342.     ' Read the number of frames.
  343.     Input #fnum, NumFrames
  344.     ReDim Frames(1 To NumFrames)
  345.     sbarFrame.Max = NumFrames
  346.     ' Read the parameters for each frame.
  347.     For i = 1 To NumFrames
  348.         Frames(i).FileInput fnum
  349.     Next i
  350.     Close fnum
  351.     SelectFrame 1
  352.     mnuFrameDelete.Enabled = (NumFrames > 1)
  353.     FileName = file_name
  354.     FileTitle = file_title
  355.     DataModified = False
  356.     Caption = "TweenRob [" & FileTitle & "]"
  357.     Exit Sub
  358. LoadScriptError:
  359.     Beep
  360.     MsgBox "Error loading file " & file_name & "." & _
  361.         vbCrLf & Format$(Err.Number) & " : " & _
  362.         Err.Description
  363.     Exit Sub
  364. End Sub
  365. ' Select and display the indicated frame.
  366. Private Sub SelectFrame(index As Integer)
  367.     SelectedFrame = index
  368.     SelectingFrame = True
  369.     sbarFrame.Value = index
  370.     SelectingFrame = False
  371.     lblFrame.Caption = Format$(index) & _
  372.         "/" & Format$(NumFrames)
  373.     DrawSelected
  374. End Sub
  375. ' Set the point that anchors the selected control
  376. ' point. For example, when moving a hand the
  377. ' corresponding elbow is the control point.
  378. Private Sub SetAnchor()
  379.     Select Case DragPoint
  380.         Case part_Head  ' The head has no anchor.
  381.             AnchorX = -1
  382.         Case part_Lelbow, part_RElbow
  383.             Frames(SelectedFrame).Position _
  384.                 part_Shoulders, AnchorX, AnchorY
  385.         Case part_LHand
  386.             Frames(SelectedFrame).Position _
  387.                 part_Lelbow, AnchorX, AnchorY
  388.         Case part_RHand
  389.             Frames(SelectedFrame).Position _
  390.                 part_RElbow, AnchorX, AnchorY
  391.         Case part_LKnee, part_RKnee
  392.             Frames(SelectedFrame).Position _
  393.                 part_Hips, AnchorX, AnchorY
  394.         Case part_LFoot
  395.             Frames(SelectedFrame).Position _
  396.                 part_LKnee, AnchorX, AnchorY
  397.         Case part_RFoot
  398.             Frames(SelectedFrame).Position _
  399.                 part_RKnee, AnchorX, AnchorY
  400.     End Select
  401. End Sub
  402. ' Grab the nearest control point within distance
  403. ' Near of the mouse.
  404. Private Sub picCanvas_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  405. Dim i As Integer
  406. Dim best_i As Integer
  407. Dim best_dist As Long
  408. Dim dx As Long
  409. Dim dy As Long
  410. Dim dist As Long
  411. Dim fx As Integer
  412. Dim fy As Integer
  413.     ' Find the closest control point.
  414.     best_dist = Near + 1
  415.     For i = part_MinPart To part_MaxControlPart
  416.         Frames(SelectedFrame).Position i, fx, fy
  417.         dx = x - fx
  418.         dy = y - fy
  419.         dist = Sqr(dx * dx + dy * dy)
  420.         If best_dist > dist Then
  421.             best_dist = dist
  422.             best_i = i
  423.         End If
  424.     Next i
  425.     ' If nothing is close enough, leave.
  426.     If best_dist > Near Then
  427.         Beep
  428.         Exit Sub
  429.     End If
  430.     ' Begin moving the control point.
  431.     Dragging = True
  432.     DragPoint = best_i
  433.     picCanvas.DrawMode = vbInvert
  434.     SetAnchor
  435.     DragX = x
  436.     DragY = y
  437.     DrawDrag
  438. End Sub
  439. ' Continue dragging a control point.
  440. Private Sub picCanvas_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  441.     If Not Dragging Then Exit Sub
  442.     ' Erase the old highlight.
  443.     DrawDrag
  444.     ' Draw the new highlight.
  445.     AdjustPoint x, y
  446.     DrawDrag
  447. End Sub
  448. ' Finish dragging the control point.
  449. Private Sub picCanvas_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
  450.     If Not Dragging Then Exit Sub
  451.     Dragging = False
  452.     ' Erase the old highlight.
  453.     DrawDrag
  454.     picCanvas.DrawMode = vbCopyPen
  455.     ' Adjust the control point.
  456.     AdjustPoint x, y
  457.     Frames(SelectedFrame).MoveControlPoint _
  458.         DragPoint, AnchorX, AnchorY, DragX, DragY
  459.     DrawSelected
  460.     DataModified = True
  461.     Caption = "TweenRob*[" & FileTitle & "]"
  462. End Sub
  463. ' Play the animation.
  464. Private Sub cmdPlay_Click()
  465.     If Playing Then
  466.         Playing = False
  467.         cmdPlay.Caption = "Stopped"
  468.         cmdPlay.Enabled = False
  469.     Else
  470.         Playing = True
  471.         cmdPlay.Caption = "Stop"
  472.         PlayData
  473.         cmdPlay.Caption = "Play"
  474.         Playing = False
  475.         cmdPlay.Enabled = True
  476.         DrawSelected
  477.     End If
  478. End Sub
  479. ' Play the animation.
  480. Private Sub PlayData()
  481. Dim ms_per_frame As Long
  482. Dim start_time As Single
  483. Dim stop_time As Single
  484.     ' See how fast we should go.
  485.     If Not IsNumeric(txtFramesPerSecond.Text) Then _
  486.         txtFramesPerSecond.Text = "10"
  487.     ms_per_frame = 1000 \ CLng(txtFramesPerSecond.Text)
  488.     ' See what kind of animation this should be.
  489.     NumPlayed = 0
  490.     start_time = Timer
  491.     If optPlay(0).Value Then
  492.         PlayDataOnce ms_per_frame
  493.     ElseIf optPlay(1).Value Then
  494.         PlayDataLooping ms_per_frame
  495.     ElseIf optPlay(2).Value Then
  496.         PlayDataBackAndForth ms_per_frame
  497.     End If
  498.     stop_time = Timer
  499.     MsgBox "Displayed" & Str$(NumPlayed) & _
  500.         " frames in " & _
  501.         Format$(stop_time - start_time, "0.00") & _
  502.         " seconds (" & _
  503.         Format$(NumPlayed / (stop_time - start_time), "0.00") & _
  504.         " FPS)."
  505. End Sub
  506. ' Play the animation once.
  507. Private Sub PlayDataOnce(ByVal ms_per_frame As Long)
  508. Dim next_time As Long
  509. Dim frame As Integer
  510.     ' Start the animation.
  511.     next_time = GetTickCount()
  512.     ' Show the frames once.
  513.     For frame = 1 To NumFrames
  514.         If Not Playing Then Exit For
  515.         NumPlayed = NumPlayed + 1
  516.         ' Draw the frame.
  517.         picCanvas.Cls
  518.         Frames(frame).Draw picCanvas, False
  519.         ' Wait until it's time for the next frame.
  520.         next_time = next_time + ms_per_frame
  521.         WaitTill next_time
  522.     Next frame
  523. End Sub
  524. ' Play the animation in a loop.
  525. Private Sub PlayDataLooping(ByVal ms_per_frame As Long)
  526.     Do While Playing
  527.         PlayDataOnce ms_per_frame
  528.     Loop
  529. End Sub
  530. ' Play the animation backward and forward.
  531. Private Sub PlayDataBackAndForth(ByVal ms_per_frame As Long)
  532.     Do While Playing
  533.         PlayDataOnce ms_per_frame
  534.         If Not Playing Then Exit Do
  535.         PlayDataBackwards ms_per_frame
  536.     Loop
  537. End Sub
  538. ' Play the animation once backwards.
  539. Private Sub PlayDataBackwards(ByVal ms_per_frame As Long)
  540. Dim next_time As Long
  541. Dim frame As Integer
  542.     ' Start the animation.
  543.     next_time = GetTickCount()
  544.     ' Show the frames once.
  545.     For frame = NumFrames To 1 Step -1
  546.         If Not Playing Then Exit For
  547.         NumPlayed = NumPlayed + 1
  548.         ' Draw the frame.
  549.         picCanvas.Cls
  550.         Frames(frame).Draw picCanvas, False
  551.         ' Wait until it's time for the next frame.
  552.         next_time = next_time + ms_per_frame
  553.         WaitTill next_time
  554.     Next frame
  555. End Sub
  556. Private Sub Form_Load()
  557.     dlgFile.Filter = _
  558.         "Robot Files (*.rob)|*.rob|" & _
  559.         "All Files (*.*)|*.*"
  560.         dlgFile.InitDir = App.Path
  561.     ' Create a single default frame.
  562.     NumFrames = 1
  563.     ReDim Frames(1 To NumFrames)
  564.     With Frames(1)
  565.         .SetParameters _
  566.             picCanvas.ScaleWidth / 2, _
  567.             (picCanvas.ScaleHeight - .MaxHeight) / 2 + _
  568.                 .HeadRoom, _
  569.             210, -30, 150, 30, 240, -60, 255, -75
  570.     End With
  571.     ' Position the scroll bar.
  572.     sbarFrame.Top = picCanvas.Top + picCanvas.Height + 1
  573.     SelectFrame 1
  574. End Sub
  575. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  576.     Cancel = Not DataSafe()
  577. End Sub
  578. Private Sub Form_Unload(Cancel As Integer)
  579.     End
  580. End Sub
  581. Private Sub mnuFileExit_Click()
  582.     Unload Me
  583. End Sub
  584. ' Load a robot script file.
  585. Private Sub mnuFileOpen_Click()
  586. Dim file_name As String
  587.     If Not DataSafe() Then Exit Sub
  588.     ' Allow the user to pick a file.
  589.     On Error Resume Next
  590.     dlgFile.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  591.     dlgFile.ShowOpen
  592.     If Err.Number = cdlCancel Then
  593.         Exit Sub
  594.     ElseIf Err.Number <> 0 Then
  595.         Beep
  596.         MsgBox "Error selecting file.", , vbExclamation
  597.         Exit Sub
  598.     End If
  599.     On Error GoTo 0
  600.     file_name = Trim$(dlgFile.FileName)
  601.     dlgFile.InitDir = Left$(file_name, Len(file_name) _
  602.         - Len(dlgFile.FileTitle) - 1)
  603.     ' Load the robot script file.
  604.     LoadScript file_name, dlgFile.FileTitle
  605. End Sub
  606. ' Save the robot script file.
  607. Private Sub mnuFileSave_Click()
  608.     If FileName = "" Then
  609.         mnuFileSaveAs_Click
  610.         Exit Sub
  611.     End If
  612.     SaveScript FileName, FileTitle
  613. End Sub
  614. ' Save the robot script file with a new name.
  615. Private Sub mnuFileSaveAs_Click()
  616. Dim file_name As String
  617.     ' Allow the user to pick a file.
  618.     On Error Resume Next
  619.     dlgFile.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  620.     dlgFile.ShowSave
  621.     If Err.Number = cdlCancel Then
  622.         Exit Sub
  623.     ElseIf Err.Number <> 0 Then
  624.         Beep
  625.         MsgBox "Error selecting file.", , vbExclamation
  626.         Exit Sub
  627.     End If
  628.     On Error GoTo 0
  629.     file_name = Trim$(dlgFile.FileName)
  630.     dlgFile.InitDir = Left$(file_name, Len(file_name) _
  631.         - Len(dlgFile.FileTitle) - 1)
  632.     ' Save the robot script file.
  633.     SaveScript file_name, dlgFile.FileTitle
  634. End Sub
  635. ' Insert a frame next to the selected one.
  636. Private Sub AddFrame()
  637. Dim i As Integer
  638.     NumFrames = NumFrames + 1
  639.     ReDim Preserve Frames(1 To NumFrames)
  640.     For i = NumFrames - 1 To SelectedFrame Step -1
  641.         Frames(i + 1).CopyFrame Frames(i)
  642.     Next i
  643.     sbarFrame.Max = NumFrames
  644.     mnuFrameDelete.Enabled = (NumFrames > 1)
  645. End Sub
  646. ' Insert a frame after the selected one.
  647. Private Sub mnuFrameAfter_Click()
  648.     AddFrame
  649.     SelectFrame SelectedFrame + 1
  650. End Sub
  651. ' Insert a frame before the selected one.
  652. Private Sub mnuFrameBefore_Click()
  653.     AddFrame
  654. End Sub
  655. ' Delete the selected frame.
  656. Private Sub mnuFrameDelete_Click()
  657. Dim i As Integer
  658.     For i = SelectedFrame To NumFrames - 1
  659.         Frames(i).CopyFrame Frames(i + 1)
  660.     Next i
  661.     NumFrames = NumFrames - 1
  662.     ReDim Preserve Frames(1 To NumFrames)
  663.     sbarFrame.Max = NumFrames
  664.     If SelectedFrame > NumFrames Then _
  665.        SelectedFrame = NumFrames
  666.     SelectFrame SelectedFrame
  667.     mnuFrameDelete.Enabled = (NumFrames > 1)
  668. End Sub
  669. ' Repaint the current frame.
  670. Private Sub picCanvas_Paint()
  671.     If SelectingFrame Then Exit Sub
  672.     SelectFrame sbarFrame.Value
  673. End Sub
  674. ' Select a new frame.
  675. Private Sub sbarFrame_Change()
  676.     If SelectingFrame Then Exit Sub
  677.     SelectFrame sbarFrame.Value
  678. End Sub
  679. ' Select a new frame.
  680. Private Sub sbarFrame_Scroll()
  681.     sbarFrame_Change
  682. End Sub
  683.